'''
Created on Apr 11, 2011

@author: yc30v1m
'''
import os
import glob
from library.Utils import Utils
from plugins.Log import Log
from ConfigParser import ConfigParser


class IniConfReader(object):
    '''
    classdocs
    '''
    __configFolder__ = 'configuration'
    __fileExt__ = 'ini'
    __absConfigFolder__ = ''
    
    __appBasePath__ = '' 
    __parser = ''
    log = Log().getLogger("IniConfReader")
    
    
    def __init__(self, appBasePath = None):
        if appBasePath == None:
            self.__appBasePath__ = Utils().baseAppPath()
            self.log.debug('Using default base path : ' + str (self.__appBasePath__))
        else:
            self.__appBasePath__ = appBasePath
            self.log.debug('Using parameterized base path : ' + str (self.__appBasePath__))
        self.setConfigFolder(self.__appBasePath__)
        self.__parser = ConfigParser()
        self.__readAllConfigurations()
        
    def getOptionsFromSection(self,sectionName, optionName):
        if self.__parser.has_option(sectionName, optionName):
            return self.__parser.get(sectionName, optionName)
        self.log.debug('Section and Options does not exist : ' + sectionName + ' Option : ' + optionName )
        return False
    
    def getSection(self,sectionName):
        if self.__parser.has_section(sectionName):
            return self.__parser.items(sectionName)
        self.log.debug('Section Does not exist : ' + str(sectionName))
        return False
    
    def __readAllConfigurations(self):
        iniList = glob.glob(self.__absConfigFolder__ + '*.' + self.__fileExt__)
        self.log.debug('Retrived Configuration Files are: ' + str(iniList))
        self.__parser.read(iniList)
        
    def setConfigFolder(self,path):
        self.__absConfigFolder__ = path + self.__configFolder__ + os.sep
        
    